package cn.yanjr.system.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yanjr.common.CurrentInfoUtil;
import cn.yanjr.common.bean.BeanFactory;
import cn.yanjr.common.cache.CacheManager;
import cn.yanjr.common.utils.Result;
import cn.yanjr.common.utils.StringUtils;
import cn.yanjr.common.web.BaseServlet;
import cn.yanjr.system.entity.SysMenu;
import cn.yanjr.system.entity.SysUser;
import cn.yanjr.system.service.SysMenuService;
import cn.yanjr.system.service.SysUserService;

@WebServlet("/sys/user/*")
public class SysUserAction  extends BaseServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private SysUserService sysUserService = BeanFactory.getBean("sysUserService", SysUserService.class);
	private SysMenuService sysMenuService = BeanFactory.getBean("sysMenuService", SysMenuService.class);
	
	
//	private SysRoleService sysRoleService;
//	private SysMenuService sysMenuService;
//	private SysOfficeService sysOfficeService;
//	private SysUserService sysUserService;
	
	@SuppressWarnings("unchecked")
	public void menus(HttpServletRequest request, HttpServletResponse response) 
			throws IOException, ServletException {
		
		String userId = request.getHeader("loginId");
		SysUser user = (SysUser) CacheManager.get("user_" + userId);
		SysMenu currTopMenu = (SysMenu) CacheManager.get("currTopMenu_" + userId);
		SysMenu currMenu = (SysMenu) CacheManager.get("currMenu_" + userId);
		
		String meunPid = request.getParameter("meunPid");
		if(StringUtils.isNotEmpty(meunPid)) {
			CacheManager.put("currTopMenu_" + userId, sysMenuService.get(meunPid));
		}
		
		List<SysMenu> list = (List<SysMenu>) CacheManager.get("userMenus_" + userId);
		if(list == null || list.size() ==0 ) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("userId", userId);
			Map<String, Object> map = sysUserService.userMenus(param);
			list = (List<SysMenu>) map.get("list");
			CacheManager.put("userMenus_" + userId, list);
		}
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("list", list);
		result.put("currTopMenu", currTopMenu);
		result.put("currMenu", currMenu);
		result.put("user", user);
		
		CurrentInfoUtil.responsePrint(response, Result.success(result));
		
	}

}
